﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using GalaSoft.MvvmLight.Messaging;
using ShoppingList.ViewModel;
using Microsoft.Phone.Tasks;

namespace ShoppingList
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            Messenger.Default.Register<string>(this, "NavigationRequest",
          s =>
          {
              if (s == "shopping")
              {
                  NavigationService.Navigate(new Uri("/Views/ShoppingsView.xaml", UriKind.Relative));
                  if (ViewModelLocator.ViewModelShoppingStatic != null) { ViewModelLocator.ClearViewModelShopping(); }
                
                  ViewModelLocator.CreateViewModelShopping();
              }

              if (s == "products")
              {
                  NavigationService.Navigate(new Uri("/Views/ProductsView.xaml", UriKind.Relative));
                  if (ViewModelLocator.ViewModelProductsStatic != null) { ViewModelLocator.ClearViewModelProducts(); }

                  ViewModelLocator.CreateViewModelProducts();
              }

              if (s == "statistics")
              {
                  NavigationService.Navigate(new Uri("/Views/StatisticsView.xaml", UriKind.Relative));
              }
              
          }
          );

            this.btnAbout.Click += new RoutedEventHandler(btnAbout_Click);

            if (!Helpers.DataBaseHelpers.VerifyExistDataBase())
            {

                MessageBoxResult i = MessageBox.Show("Esta es la primera vez que se ejecuta Shopping Analytics presione aceptar para crear la información necesaria");
                if (i == MessageBoxResult.OK)
                {
                    ShoppingList.Helpers.DataBaseHelpers.SetupDataBase();
                    ShoppingList.Helpers.ShopListHelpers.VerifyExistShopList();
                }
            }

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {

            panorama.Background = Helpers.ShopListHelpers.DisplayState();

        }




        void btnAbout_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
                marketplaceReviewTask.Show();
            }
            catch( Exception ex)
            {

            }
           
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {


                this.NavigationService.RemoveBackEntry();

            }
            catch (Exception ex)
            {

            }

            if (ViewModelLocator.ViewModelStatisticsStatic != null)
            {
                ViewModelLocator.ViewModelStatisticsStatic.SelectStatisticsCommand.Execute("Esta semana");

                Helpers.DispatchService.Dispatch(() => 
                {
                    ViewModelLocator.MainStatic.SetupTiles();
                });
              
                
               // ViewModelLocator.CreateViewModelStatistics();
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
           
        }
    }
}